package Stack;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;

public class _94_BinaryTreeInorderTraversal {
    Stack<TreeNode> stack = new Stack();
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        helper(root);
        while (!stack.isEmpty()) {
            res.add(stack.pop().val);
        }
        Collections.reverse(res);
        return res;
    }

    public void helper(TreeNode root) {
        if(root==null) return;
        if (root.left != null) {
            helper(root.left);
        }
        stack.add(root);
        if (root.right != null) {
            helper(root.right);
        }
    }
}
